package com.zhongketech.sunflower.secondfragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zhongketech.sunflower.Constant;
import com.zhongketech.sunflower.MyApplication;
import com.zhongketech.sunflower.MyToast;
import com.zhongketech.sunflower.R;
import com.zhongketech.sunflower.adapter.FcsContact;
import com.zhongketech.sunflower.adapter.FcsContactAdapter;
import com.zhongketech.sunflower.transport.Action;
import com.zhongketech.sunflower.transport.model.Authorizations;
import com.zhongketech.sunflower.transport.model.SpecialNums;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class BaseFragment extends Fragment implements OnClickListener {
	protected View mMainView;
	protected Context mContext;
	private ListView listView ;
	private List<FcsContact> familyData = new ArrayList<FcsContact>();
	private List<FcsContact> emergencyData = new ArrayList<FcsContact>();
	private Handler handler ;
	private FcsContactAdapter adapter;

	private String type = Constant.SN_FAMILY;

	public BaseFragment() {
		super();
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mContext = activity.getApplicationContext();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mMainView = inflater.inflate(R.layout.fcs_specialnumber_fragment, container, false);

		initView();

		handler = new Handler() {
			public void handleMessage(Message msg) {
				if(type.equals(Constant.SN_FAMILY))
				{
					adapter.updateData(familyData,Constant.FCS_ADAPTER_TYPE_UNKNOWN);
				}
				else if(type.equals(Constant.SN_EMERGENCY))
				{
					adapter.updateData(emergencyData,Constant.FCS_ADAPTER_TYPE_UNKNOWN);
				}
			}
		};

		initData();

		return mMainView;
	}

	public void updateType(String type)
	{
		this.type = type;
		final Message msg = new Message();
		handler.sendMessage(msg);
	}

	private void initData()
	{

		new Thread(new Runnable() {
			public void run() {
				Looper.prepare();
				List<SpecialNums> list = new Action().specialnums();
				if(null != list)
				{
					familyData.clear();
					emergencyData.clear();
					for(int i = 0;i < list.size();i ++)
					{
						FcsContact ad = new FcsContact();
						ad.imageViewId = -1;
						ad.itemText = list.get(i).getName();
						ad.statusText = null;
						ad.textview2 =  list.get(i).getPhone();
						if(Constant.SN_FAMILY.equals(list.get(i).getType()))
						{
							familyData.add(ad);
						}
						else if(Constant.SN_EMERGENCY.equals(list.get(i).getType()))
						{
							emergencyData.add(ad);
						}

						final Message msg = new Message();
						handler.sendMessage(msg);
					}

				}

			}
		}).start();

	}

	private void initView()
	{

		listView = (ListView)mMainView.findViewById(R.id.listView);
		adapter = new FcsContactAdapter(familyData,Constant.FCS_ADAPTER_TYPE_UNKNOWN);
		listView.setAdapter(adapter);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
